




from t0mm0.common.addon import Addon

from appmanager import AppManager
from appmanager import Mode

################### Source Import #################################
from diretube import DireTube
from amharicmovies import AmharicMovies
from tgun import TGun


class MainApp():

    _media_list = []

    def __init__(self):
        self.app = AppManager()
        self.populateMediaList()
        self.play = self.app.getAddonQueries('play')
        self.mode = self.app.getAddonQueries('mode')
        self.selected_media = self.getSelectedMedia()
        url = self.app.getAddonQueries('url')
        print 'Mode: ' + str(self.mode)
        print 'Play: ' + str(self.play)
        print 'URL: ' + str(url)
        self.run()
        
    def run(self):        
        if self.mode == 'main':
            self.displayMainMenu()
        else:
            self.handleMode()          
        if not self.play:
            self.app.endAddingDirectory()
        else:
            self.app.getAddon().resolve_url(self.app.getURL())
            
    def handleMode(self):
        import time
        if self.mode == str(Mode.MAINMENUSELECTED):
            print 'Media Selected'
            start = time.clock()
            self.selected_media.displayMainMenu()
            print 'Time Taken'
            print time.clock()- start            
        elif self.mode == str(Mode.SUBMENUSELECTED):
            print 'item selected'
            self.selected_media.displaySubMenu()            
        elif self.mode == str(Mode.NEXT):
            print 'item selected'
            self.selected_media.displayNextPage()   
        print "im in handle mode"
       
    def getSelectedMedia(self):
        print 'getting selected media'
        print 'number of medias'
        print len(MainApp._media_list)
        for media in MainApp._media_list:
            print media.getName()
            print self.app.getName()
            if str(media.getName()) == str(self.app.getName()):
                print media.getName()
                return media

    def displayMainMenu(self):
        for each_media in MainApp._media_list:
            self.app.getAddon().add_directory({'mode': Mode.MAINMENUSELECTED,'name':each_media.getName()}, {'title': each_media.getName()},img=each_media.getIcon(),fanart=each_media.getFanart(),total_items=len(MainApp._media_list))
        
    def populateMediaList(self):
        MainApp._media_list.append(DireTube(self.app))
        MainApp._media_list.append(AmharicMovies(self.app))
        MainApp._media_list.append(TGun(self.app))


